#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@IDE     ：PyCharm 
@Author  ：kiway
@Date    ：2024/5/3 10:24 
"""
import asyncio
import multiprocessing
import time

"""定时任务"""

from multiprocessing import Process
from asyncio import sleep as asleep


class LoopRunTask(object):
    """Loop run task.
    """

    def __init__(self):
        self.__tasks = []

    async def while_run(self, func, interval, args):
        while True:
            await func(*args)
            await asleep(interval)

    def register(self, func, interval, *args):
        self.__tasks.append((func, interval, args))

    def _run(self):
        loop = asyncio.new_event_loop()
        for (func, interval, args) in self.__tasks:
            loop.create_task(self.while_run(func, interval, args))

        loop.run_forever()

    def run(self):

        Process(target=self._run).start()
